package com.example.yang.yixin.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.yang.yixin.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class DetialsActivity extends BaseActivity {
View v;
private ImageView mIv_back;
TextView text_bkpassword,text_classname,text_coursename,text_requested,text_sign,text_phone;
SharedPreferences pref;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detials);
        mIv_back=findViewById(R.id.iv_back);
        mIv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(DetialsActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
        initView();

    }






    private void initView() {
        initNarvBar(true, "详情", true);
        initBottombar(v);
        text_bkpassword = (TextView) findViewById(R.id.edit_bkpassword);
        text_classname = (TextView) findViewById(R.id.edit_classname);
        text_coursename = (TextView) findViewById(R.id.edit_cname);
        text_requested = (TextView) findViewById(R.id.edit_study_request);
        text_sign = (TextView) findViewById(R.id.edit_sign);
        text_phone = (TextView) findViewById(R.id.edit_phone);
        pref=getSharedPreferences("user",MODE_PRIVATE);
        String bkpassword=pref.getString("bkpassword",null);
        getServerData(bkpassword);






    }


    private void getServerData(final String bkpassword) {
        String jsonurl = "http://47.106.141.161/MyFirstAndroidApp/InformationServlet?bkpassword="+bkpassword;
        RequestQueue mQueue = Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(jsonurl, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {


                    String cname=response.getString("cname");
                    String classname=response.getString("classname");
                    String requested=response.getString("requested");
                    String sign=response.getString("sign");
                    String phone=response.getString("phone");
                    text_bkpassword.setText(bkpassword);
                    text_classname.setText(classname);
                    text_coursename.setText(cname);
                    text_requested.setText(requested);
                    if(sign.equals("0")){
                        text_sign.setText("未开启");
                    }else{
                        text_sign.setText("已开启");
                    }
                    if(phone.equals("0")){
                        text_phone.setText("未开启");
                    }else{
                        text_phone.setText("已开启");
                    }









                } catch (JSONException e) {

                    Log.e("json错误",e.getMessage());
                }

            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e("TAG错误", volleyError.getMessage(), volleyError);
                    }
                }) {
            protected Response<JSONObject> paraeNetworkResponse(NetworkResponse response) {
                try {
                    JSONObject jsonObject = new JSONObject(new String(response.data, "utf-8"));
                    return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
                }catch (JSONException je) {
                    return Response.error(new ParseError(je));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                }

            }
        };


        mQueue.add(jsonObjectRequest);
    }


}
